package com.cashmama.market.api.application.utils;

import com.cashmama.market.api.infras.app.RequestBasicInfo;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fqgj.common.utils.JSONUtils;
import com.fqgj.common.utils.MD5;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Arrays;
import java.util.Map;

/**
 * @author poyangchen
 * 验签工具类
 */
public class AppSignUtil {
    private static final Logger logger = LoggerFactory.getLogger(AppSignUtil.class);


    private final static String appTestKey = "123456";
    private final static String appProdKey = "90016";
    private final static String appTestSecret = "460bd2ae05a0756a443b72778a9e96b9";
    private final static String appProdSecret = "9d19fa1115312b5415333218b0e0d122";

    /**
     * 验签
     *
     * @param basicVO      基础参数
     * @param businessJSON 业务参数
     * @param serverTest   是否是测试服
     * @return
     */
    public static boolean verifyAppSign(RequestBasicInfo basicVO, String businessJSON, boolean serverTest) {
        basicVO.validate();

        if (serverTest) {
            return true;
//            return checkSign(basicVO, businessJSON, appTestKey, appTestSecret);
        }

        return checkSign(basicVO, businessJSON, appProdKey, appProdSecret) || checkSign(basicVO, businessJSON, appTestKey, appTestSecret);
    }


    private static boolean checkSign(RequestBasicInfo basicVO, String businessJSON,
                                     String appkey, String appSecret) {

        if (!appkey.equals(basicVO.getAppKey())) {
            return false;
        }
        StringBuilder signStr = getBasicVerifyString(basicVO);
        signStr.append(appSecret);
        String sign = MD5.md5(MD5.md5(signStr.toString()));
        return sign.equals(basicVO.getAppSign());
    }


    /**
     * 公共参数是写死的拼接
     *
     * @param basicVO
     * @return
     */
    private static StringBuilder getBasicVerifyString(RequestBasicInfo basicVO) {
        StringBuilder signStr = new StringBuilder();
        signStr.append(basicVO.getAppKey());
        signStr.append(basicVO.getAppVersion());
        signStr.append(basicVO.getAppClient());
        signStr.append(basicVO.getVersionCode());
        signStr.append(basicVO.getTimestamp() + "");
        if (StringUtils.isNotEmpty(basicVO.getGuestId())) {
            signStr.append(basicVO.getGuestId());
        }
        if (StringUtils.isNotEmpty(basicVO.getToken())) {
            signStr.append(basicVO.getToken());
        }
        return signStr;
    }

    /**
     * 业务参数排序拼接
     *
     * @param paramMap
     * @return
     */
    public static String map2SortStr(Map<String, Object> paramMap) {

        // 对参数名进行字典排序
        String[] keyArray = paramMap.keySet().toArray(new String[0]);
        Arrays.sort(keyArray);
        // 拼接有序的参数名-值串
        StringBuilder stringBuilder = new StringBuilder();
        for (String key : keyArray) {
            try {
                String value = JSONUtils.getInstance().writeValueAsString(paramMap.get(key));
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }
            stringBuilder.append(key).append(paramMap.get(key));
        }

        return stringBuilder.toString();
    }

}
